﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace ConsoleApplication7
{
    /// <summary>
    /// Basic XML configuration class
    /// </summary>
    public class XMLConfig
    {
        private XmlDocument _xml = new XmlDocument();

        public XMLConfig(String cfgFile)
        {
            _xml.Load(cfgFile);
        }

        public XmlDocument BaseXml
        {
            get { return _xml; }
        }

        /// <summary>
        /// Basic element in configuration
        /// </summary>
        public class Element
        {
            private XmlElement _elem = null;

            public Element(XmlElement elem)
            {
                _elem = elem;
            }

            /// <summary>
            /// Base xml element
            /// </summary>
            public XmlElement BaseElement
            {
                get { return _elem; }
            }

            /// <summary>
            /// Xml element tag name
            /// </summary>
            public String Tag
            {
                get { return _elem.Name; }
            }

            /// <summary>
            /// Xml attributes
            /// </summary>
            public String GetAttribute(String attr)
            {
                XmlAttribute a = _elem.Attributes[attr];
                if (a == null)
                    return "";
                else
                    return a.Value;
            }

            /// <summary>
            /// Get content of element
            /// </summary>
            /// <returns></returns>
            public String GetContent()
            {
                StringBuilder str = new StringBuilder();
                for (int i = 0; i < _elem.ChildNodes.Count; i++)
                {
                    XmlNode node = _elem.ChildNodes[i];
                    if (node is XmlText)
                    {
                        XmlText txt = node as XmlText;
                        str.Append(txt.Value);
                    }
                    else if (node is XmlCDataSection)
                    {
                        XmlCDataSection data = node as XmlCDataSection;
                        str.Append(data.Value);
                    }
                }
                return str.ToString();
            }
        }
    }
}
